//creare comments pe client
//varianta compatibila doar mozilla

function xmlChapterComments(chapId, url){
	
	/**
	*	constructor
	*   @param String chapId atributul id al noului cap
	*   @return void
	*/
	var docType=document.implementation.createDocumentType("chapterComments", null, "../../include/dtd/xmlChapterComments.dtd");
	this.commentsHandler=document.implementation.createDocument(null, "chapterComments", docType);

	this.commentsHandler.async=false;

	this.newElemQueue=new Array();
	this.newElemQueueCounter=0;
	this.changesQueue=new Array();
	this.changesQueueCounter=0;

	if(url == null){
		var basicStruct="<chapterComments chapterId='" +chapId+"'></chapter>";
		try{
			var newParser=new DOMParser();
			var newXmlDom=newParser.parseFromString(basicStruct, "text/xml");

			this.commentsHandler.removeChild(this.commentsHandler.documentElement);

			for(var i=0; i< newXmlDom.childNodes.length; i++){
				var newNode=this.commentsHandler.importNode(newXmlDom.childNodes[i], true);
				this.commentsHandler.appendChild(newNode);
			}

			this.commentsRoot=this.commentsHandler.documentElement;
			this.chapId=chapId;
		}catch(e){
			alert("catch message: "+e.message);
		}
	}else{
		//load from source
		this.chapId=chapId;
		this.commentsHandler.load(url);
		this.commentsRoot=this.commentsHandler.documentElement;
		this.commentsURL=url;
	}
	
	/**
	*	creare comentariu nou
	*   @param String id id'ul noului comentariu
	*   @param String author autorul comentariului
	*   @param String elemId id'ul elementului comentat
	*   @param String date
	*   @param String content
	*/
	this.createNewComment=function(id, author, elemId, date, content){
		//helder
		var chapterComment=this.commentsHandler.createElement('chapterComment');
		chapterComment.setAttribute('commentId', id);
		
		var commentAuthor=this.commentsHandler.createElement('commentAuthor');
		commentAuthor.appendChild(this.commentsHandler.createTextNode(author));
		chapterComment.appendChild(commentAuthor);
		
		var commentElementId=this.commentsHandler.createElement('commentElementId');
		commentElementId.appendChild(this.commentsHandler.createTextNode(elemId));
		chapterComment.appendChild(commentElementId);
		
		var commentDate=this.commentsHandler.createElement('commentDate');
		commentDate.appendChild(this.commentsHandler.createTextNode(date));
		chapterComment.appendChild(commentDate);
		
		var commentContent=this.commentsHandler.createElement('commentContent');
		commentContent.appendChild(this.commentsHandler.createTextNode(content));
		chapterComment.appendChild(commentContent);
		
		this.commentsRoot.appendChild(this.commentsHandler.importNode(chapterComment, true));
		//alert("dfsdfg");
		this.newElemQueue[this.newElemQueueCounter]=chapterComment;
		this.newElemQueueCounter+=1;
	}
	
	/**
	*	update comentariu
	*   @param String id id'ul noului comentariu
	*   @param String author autorul comentariului
	*   @param String elemId id'ul elementului comentat
	*   @param String date
	*   @param String content
	*/
	this.updateComment=function(id, author, elemId, date, content){
		//helder
		var chapterComment=this.commentsHandler.createElement('chapterComment');
		chapterComment.setAttribute('commentId', id);
		
		var commentAuthor=this.commentsHandler.createElement('commentAuthor');
		commentAuthor.appendChild(this.commentsHandler.createTextNode(author));
		chapterComment.appendChild(commentAuthor);
		
		var commentElementId=this.commentsHandler.createElement('commentElementId');
		commentElementId.appendChild(this.commentsHandler.createTextNode(elemId));
		chapterComment.appendChild(commentElementId);
		
		var commentDate=this.commentsHandler.createElement('commentDate');
		commentDate.appendChild(this.commentsHandler.createTextNode(date));
		chapterComment.appendChild(commentDate);
		
		var commentContent=this.commentsHandler.createElement('commentContent');
		commentContent.appendChild(this.commentsHandler.createTextNode(content));
		chapterComment.appendChild(commentContent);
		
		this.commentsRoot.appendChild(this.commentsHandler.importNode(chapterComment, true));
		
		this.changesQueue[this.changesQueueCounter]=chapterComment;
		this.changesQueueCounter+=1;
	}
}